package com.ezbuy.common.helper;

import android.util.Log;

import com.ezbuy.common.BuildConfig;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * @author yutianran
 * @version 1.0
 * @ClassName: LogUtils
 * @Description: 日志打印工具类
 * @date 2018-10-11
 */
public class LogUtils {
    /**
     * 默认的TAG
     */
    private static final String GLOBAL_TAG = "GLOBAL_TAG";
    /**
     * 默认level为debug级别
     */
    private static final int LEVEL = Log.DEBUG;
    /**
     * Debug时，显示log
     */
    public static boolean SHOW_LOG = BuildConfig.DEBUG;

    public static Gson gson = new GsonBuilder().disableHtmlEscaping().create();

    public static void v(String tag, String msg) {
        performPrint(Log.VERBOSE, tag, msg);
    }

    public static void d(String tag, String msg, Throwable tr) {
        performPrint(Log.DEBUG, tag, msg + " " + Log.getStackTraceString(tr));
    }

    public static void d(String msg) {
        performPrint(Log.DEBUG, GLOBAL_TAG, msg);
    }

    public static void d(String tag, String msg) {
        performPrint(Log.DEBUG, tag, msg);
    }

    public static void i(String tag, String msg) {
        performPrint(Log.INFO, tag, msg);
    }

    public static void i(String tag, String msg, Throwable tr) {
        performPrint(Log.INFO, tag, msg + " " + Log.getStackTraceString(tr));
    }

    public static void w(String tag, String msg) {
        performPrint(Log.WARN, tag, msg);
    }

    public static void e(String tag, String msg) {
        performPrint(Log.ERROR, tag, msg);
    }

    public static void e(String tag, String msg, Throwable tr) {
        performPrint(Log.ERROR, tag, msg + "\n" + Log.getStackTraceString(tr));
    }

    public static void json(String tag, String json) {
        performPrint(Log.ERROR, tag, JsonFormatUtil.format(json));
    }


    public static void object(String tag, Object obj) {
        if (obj != null) {
            performPrint(LEVEL, tag, JsonFormatUtil.format(gson.toJson(obj)));
        } else {
            performPrint(LEVEL, tag, "null");
        }
    }

    /**
     * 打印异常信息
     */
    public static void printException(Throwable throwable) {
        performPrint(Log.ERROR, "AndroidRuntimeDebug", Log.getStackTraceString(throwable));
    }

    /**
     * 打印类名TAG的日志
     *
     * @param msg
     */
    public static void print(String msg) {
        performPrint(LEVEL, "", msg);
    }

    /**
     * 打印默认TAG的日志
     *
     * @param msg
     */
    public static void printDefaluTag(String msg) {
        performPrint(LEVEL, GLOBAL_TAG, msg);
    }

    /**
     * 打印自定义Tag的日志
     *
     * @param tag
     * @param msg
     */
    public static void print(String tag, String msg) {
        performPrint(LEVEL, tag, msg);
    }

    /**
     * 打印自定义Tag,自定义Level的日志
     *
     * @param level
     * @param tag
     * @param msg
     */
    public static void print(int level, String tag, String msg) {
        performPrint(level, tag, msg);
    }

    /**
     * 具体执行打印的逻辑,如果TAG为空,则自动以所在的类为TAG
     */
    private static void performPrint(int level, String tag, String msg) {
        //隐藏日志，则直接返回，不再打印
        if (!SHOW_LOG) {
            return;
        }
        String threadName = Thread.currentThread().getName();
        //3代表方法的调用深度：0-performPrint，1-各种封装好的print方法，2-调用该工具类的方法位置
        StackTraceElement element = ((new Exception()).getStackTrace())[2];
        String lineIndicator = new StringBuilder("(")
                .append(element.getFileName()).append(":")
                .append(element.getLineNumber()).append(").")
                .append(element.getMethodName()).append(":")
                .toString();
        if (tag == null || tag.length() == 0) {
            tag = element.getFileName();
        }
        String log = lineIndicator + " " + threadName + " " + msg;
        segmentPrint(level, tag, log);
    }

    /**
     * 分段输出日志,解决Logcat日志打印不全的问题
     */
    public static void segmentPrint(int level, String tag, String msg) {
        if (tag == null || tag.length() == 0 || msg == null || msg.length() == 0) {
            return;
        }
        int segmentSize = 3 * 1024;
        long length = msg.length();
        if (length <= segmentSize) {
            // 长度小于等于限制直接打印
            Log.println(level, tag, msg);
        } else {
            // 循环分段打印日志
            while (msg.length() > segmentSize) {
                String logContent = msg.substring(0, segmentSize);
                msg = msg.replace(logContent, "");
                Log.println(level, tag, logContent);
            }
            // 打印剩余日志
            Log.println(level, tag, msg);
        }
    }

}
